package neuronet.common;

import java.util.Vector;

import org.apache.log4j.Logger;

public class NeuroNetCom {

    final static String CR = System.getProperty("line.separator");

	// Logger
	private static Logger logger = Logger.getLogger(NeuroNetCom.class);

	public static String[] getParameterArrayFormMessageX(String para,
			String mess) {
		String data = getParameterArrayFormMessage(para, mess);
		return data.split(",");
	}

	public static String getParameterArrayFormMessage(String para, String mess) {
		// local debugflag for method
		boolean localdebug = false;

		if (localdebug) {
			logger.debug(" - getParameterArrayFormMessage(String para, String mess) wirh para = "
					+ para);
		}
		if (mess.length() == 0) {
			logger.error(" -- mess.length() == 0");
			return "-1";
		}
		// check if para is contained
		if (!mess.contains(para)) {
			logger.info(" Parameter " + para + " not contained in " + mess);
			return "";
		}
		// check if para has no elements, eg contails para+": "
		if (mess.contains(para + ": ")) {
			logger.info(" Parameter " + para + " als no elements in " + mess);
			return "";
		}

		// check for para
		int start = mess.indexOf(" " + para);
		if (start == -1)
			return "-1";

		// get para value
		start += para.length() + 1;

		// check for nested []

		int end = start;
		int open = 0;
		for (int i = start; i < mess.length(); i++) {
			if (localdebug)
				logger.debug(" -- " + i + " = " + mess.charAt(i));
			if (mess.charAt(i) == '[') {
				open++;
			}
			if (mess.charAt(i) == ']') {
				open--;
			}
			if (open == 0) {
				end = i;
				break;
			}
		}

		// if (end < start)
		// end = mess.length();

		if (localdebug)
			logger.debug(" -- mess  = " + mess);
		if (localdebug)
			logger.debug(" -- start = " + start);
		if (localdebug)
			logger.debug(" -- end   = " + end);

		String paraData = "";

		// if (mess.charAt(start) == '[')
		// paraData = mess.substring(start, end);
		// else
		paraData = mess.substring(start + 1, end);
		logger.debug(" -- dataArray:para = " + para);
		logger.debug(" -- dataArray:paraData = -|" + paraData + "|-");

		return paraData;
	}

	public static Vector<String> extractDataFromMessage(String mess) {
		return extractDataFromMessage(mess, "messages");
	}

	/**
	 * This method converts the raw multimessagestring marked by the keyword
	 * into a Vector<String> containing the separated messages. E.G.: status:ok
	 * message[[],[]]
	 * 
	 * @param mess
	 *            the raw messagestring
	 * @param keyword
	 *            the marking keyword
	 * @return
	 */
	public static Vector<String> extractDataFromMessage(String mess,
			String keyword) {
		logger.debug("*** extractDataFromMessage from -|" + mess + "|-");

		// extract data
		Vector<String> dataVec = new Vector<String>();
		String dataStr = NeuroNetCom
				.getParameterArrayFormMessage(keyword, mess);
		if (dataStr.length() > 0) {
			String[] messages = dataStr.split("\\]\\,\\[");

			messages[messages.length - 1] = messages[messages.length - 1]
					.substring(0, messages[messages.length - 1].length() - 1);
			messages[0] = messages[0].substring(1, messages[0].length());

			for (int i = 0; i < messages.length; i++) {
				logger.debug("*** " + keyword + "[" + i + "] = " + messages[i]);

				String inTime = NeuroNetCom.getParameterFormMessage(
						"incomingtime", messages[i]);
				String senderID = NeuroNetCom.getParameterFormMessage(
						"senderid", messages[i]);
				String messDataStr = NeuroNetCom.getParameterFormMessage(
						"data", messages[i]);

				// if(i==0)
				// messages[i] = messages[i].substring(1, messages[i].length());

				logger.debug("***** inTime = " + inTime);
				logger.debug("***** senderiD = " + senderID);
				logger.debug("***** message = " + messages[i]);

				String[] messDa = messDataStr.split(",");
				messDa[messDa.length - 1] = messDa[messDa.length - 1]
						.substring(0, messDa[messDa.length - 1].length() - 1);
				for (int v = 0; v < messDa.length; v++) {
					logger.debug("**** messDa[" + v + "] = " + messDa[v]);
				}

				dataVec.add(messages[i]);
			}
		} else {
			logger.debug("**** no new " + keyword + " ");
		}

		return dataVec;
	}

	public static String getMessageType(String str) {
		/**
		 * Messagetypes logon logon client e. g. first registration logoff
		 * logoff client data data transmission config configure settings
		 */

		int start = 5 + str.indexOf("type:");
		int end = str.indexOf(" ", start);
		String type = str.substring(start, end);
		if (type.length() < 1)
			type = "none";

		return type;
	}

	public static String getParameterFormMessage(String para, String mess) {
		// local debugflag for method
		boolean localdebug = false;

		if (localdebug)
			logger.debug(" - getParameterFormMessage(String para, String mess) ");
		if (mess.length() == 0) {
			logger.error(" -- mess.length() == 0");
			return "-1";
		}
		logger.debug(" - mess = " + mess);

		// check if para is contained
		if (!mess.contains(para)) {
			logger.info(" Parameter " + para + " not contained in " + mess);
			return "";
		}
		// check if para has no elements, eg contails para+": "
		if (mess.contains(para + ": ")) {
			logger.info(" Parameter " + para + " als no elements in " + mess);
			return "";
		}

		if (mess.contains(para)) {
			// check for para
			int start = mess.indexOf(para);
			if (start == -1)
				return "-1";
			if (localdebug)
				logger.debug(" -- start = " + start);
			// get para value
			start += para.length() + 1;
			int end = mess.indexOf(" ", start);
			if (end < start)
				end = mess.length();

			if (localdebug)
				logger.debug(" -- end = " + end);
			if (localdebug)
				logger.debug(" -- mess.length() = " + mess.length());

			String paraData = mess.substring(start, end);
			if (localdebug)
				logger.debug(" -- para = " + para);
			if (localdebug)
				logger.debug(" -- paraData = " + paraData);
			return paraData;
		} else {
			logger.info(" Parameter " + para + " not contained in " + mess);
			return "";
		}
	}

	public static Vector<String> getStringAsVector(String para, String sep) {
		logger.info("para is -|" + para + "|-");

		String[] paraArray = para.split(sep);
		Vector<String> dataVec = new Vector<String>();

		for (String client : paraArray) {
			dataVec.add(client);
		}

		return dataVec;
	}

	public static String stringVectorToString(Vector<String> targets, String sep) {
		String data = "";
		for (String da : targets) {
			data += da + sep;
		}
		if (data.length() > 0)
			data = data.substring(0, data.length() - 1);
		return data;
	}

	public static Vector<String> stringToVectorString(String message, String cut) {
		Vector<String> vec = new Vector<String>();
		if (message.endsWith("]") && message.startsWith("[")) {
			message = message.substring(1, message.length() - 1);
		}
		String[] buff = message.split(cut);

		for (int i = 0; i < buff.length; i++) {
			vec.add(buff[i]);
		}
		return vec;
	}
}
